﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DTTB.Common;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.FormBean.DanhMuc;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.LinQ.DanhMuc;
using Webdiyer.WebControls.Mvc;

namespace DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.DanhMucDelegate
{
    public class PhuCapDiLaiDelegate
    {
        DBDanhMucDataContext DBDanhMuc = new DBDanhMucDataContext();

        public IList<dm_PhuCapDiLai> GetAllPhuCapDiLai()
        {
            return DBDanhMuc.dm_PhuCapDiLais.Where(p => p.IsDelete == false).ToList();
        }
        public dm_PhuCapDiLai AddNewPhuCapDiLai(dm_PhuCapDiLai PhuCapDiLai)
        {
            DBDanhMuc.dm_PhuCapDiLais.InsertOnSubmit(PhuCapDiLai);

            DBDanhMuc.SubmitChanges();

            return GetLoaiCongChucIdMax();
        }

        public void DeletePhuCapDiLai(int PhuCapDiLaiID)
        {
            dm_PhuCapDiLai PhuCapDiLaiNew =
                DBDanhMuc.dm_PhuCapDiLais.Single(p => p.Id == PhuCapDiLaiID);
            //LoaiCongChuc.Id = LoaiCongChuc.Id;

            DBDanhMuc.dm_PhuCapDiLais.DeleteOnSubmit(PhuCapDiLaiNew);
            DBDanhMuc.SubmitChanges();
        }
        public int DeletePhuCapDiLai(dm_PhuCapDiLai PhuCapDiLai)
        {
            dm_PhuCapDiLai PhuCapDiLaiDelete =
                DBDanhMuc.dm_PhuCapDiLais.Single(p => p.Id == PhuCapDiLai.Id);
            if (PhuCapDiLaiDelete.hs_NhanVien_PCDiLais.Count() == 0)
            {
                PhuCapDiLaiDelete.Id = PhuCapDiLai.Id;
                PhuCapDiLaiDelete.UpdateDate = DateTime.Now;
                PhuCapDiLaiDelete.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
                PhuCapDiLaiDelete.IsDelete = true;
                DBDanhMuc.SubmitChanges();
                return 1;
            }
            else
            {
                return 0;
            }
        }
        public void UpdatePhuCapDiLai(dm_PhuCapDiLai PhuCapDiLai)
        {
            dm_PhuCapDiLai PhuCapDiLaiUpdate =
                DBDanhMuc.dm_PhuCapDiLais.Single(p => p.Id == PhuCapDiLai.Id);

            PhuCapDiLaiUpdate.Id = PhuCapDiLai.Id;
            PhuCapDiLaiUpdate.TienPhuCapDiLai = PhuCapDiLai.TienPhuCapDiLai;
            PhuCapDiLaiUpdate.UpdateDate = PhuCapDiLaiUpdate.UpdateDate;
            PhuCapDiLaiUpdate.UpdateBy = PhuCapDiLaiUpdate.UpdateBy;
            DBDanhMuc.SubmitChanges();
        }

        public PagedList<dm_PhuCapDiLai> SearchPhuCapDiLaiPagedList(int? id, string searchCriteria)// id = page Index
        {
            return (PagedList<dm_PhuCapDiLai>)DBDanhMuc.dm_PhuCapDiLais.Where(p => p.TienPhuCapDiLai == decimal.Parse(searchCriteria) && p.IsDelete == false).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);


        }

        public dm_PhuCapDiLai GetPhuCapDiLaiById(string id)
        {
            try
            {
                dm_PhuCapDiLai item = this.DBDanhMuc.dm_PhuCapDiLais.Single(p => p.Id == Convert.ToInt32(id));
                return item;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public dm_PhuCapDiLai GetPhuCapDiLaiByTenCheck(decimal ten)
        {

            IEnumerable<dm_PhuCapDiLai> list = this.DBDanhMuc.dm_PhuCapDiLais.Where(p => p.TienPhuCapDiLai == ten && p.IsDelete == false);
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }
        public dm_PhuCapDiLai GetPhuCapDiLaiByTen(decimal ten)
        {

            IEnumerable<dm_PhuCapDiLai> list = this.DBDanhMuc.dm_PhuCapDiLais.Where(p => p.TienPhuCapDiLai == ten);
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }
        public PagedList<dm_PhuCapDiLai> RecycleGetPhuCapDiLaiPagedList(int? id)// id = page Index
        {
            return DBDanhMuc.dm_PhuCapDiLais.Where(p => p.IsDelete == true).OrderByDescending(p => p.Id).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }
        public PagedList<dm_PhuCapDiLai> GetPhuCapDiLaiPagedList(int? id)// id = page Index
        {
            return DBDanhMuc.dm_PhuCapDiLais.Where(p => p.IsDelete == false).OrderByDescending(p => p.Id).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }

        public dm_PhuCapDiLai BuildNewPhuCapDiLai(PhuCapDiLaiForm form)
        {
            dm_PhuCapDiLai newPhuCapDiLai = new dm_PhuCapDiLai();
            if (!string.IsNullOrEmpty(form.TienPhuCapDiLai))
                newPhuCapDiLai.TienPhuCapDiLai = decimal.Parse(form.TienPhuCapDiLai.Replace(".", ""), null);
            newPhuCapDiLai.CreateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            newPhuCapDiLai.CreateDate = DateTime.Now;
            newPhuCapDiLai.IsDelete = false;
            return newPhuCapDiLai;
        }
        public int PhucHoiPhuCapDiLai(int id)
        {
            dm_PhuCapDiLai phuchoi = new dm_PhuCapDiLai();
            phuchoi = this.GetPhuCapDiLaiById(Convert.ToString(id));
            dm_PhuCapDiLai item = new dm_PhuCapDiLai();
            item = this.GetPhuCapDiLaiByTenCheck(phuchoi.TienPhuCapDiLai.Value);
            if (item != null)
                return 1;
            else
            {
                phuchoi.IsDelete = false;
                DBDanhMuc.SubmitChanges();
                return 2;
            }
        }
        public dm_PhuCapDiLai BuildEditPhuCapDiLai(PhuCapDiLaiForm form)
        {
            dm_PhuCapDiLai newPhuCapDiLai = new dm_PhuCapDiLai();
            newPhuCapDiLai.Id = form.id;
            if (!string.IsNullOrEmpty(form.TienPhuCapDiLai))
                newPhuCapDiLai.TienPhuCapDiLai = decimal.Parse(form.TienPhuCapDiLai.Replace(".", ""), null);
            newPhuCapDiLai.UpdateDate = DateTime.Now;
            newPhuCapDiLai.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            return newPhuCapDiLai;
        }
        public dm_PhuCapDiLai GetLoaiCongChucIdMax()
        {
            IList<dm_PhuCapDiLai> list = DBDanhMuc.dm_PhuCapDiLais.ToList();
            return list.Count != 0 ? list.ElementAt(list.Count - 1) : null;
        }
        public PhuCapDiLaiForm ConvertToForm(dm_PhuCapDiLai obj)
        {
            PhuCapDiLaiForm form = new PhuCapDiLaiForm();
            form.id = obj.Id;
            form.TienPhuCapDiLai = obj.TienPhuCapDiLai != null ? obj.TienPhuCapDiLai.Value.ToString("#,#").Replace(",", ".") : "";
            form.UpdateDate = obj.UpdateDate != null ? obj.UpdateDate.Value : DateTime.MinValue;
            form.CreateDate = obj.CreateDate != null ? obj.CreateDate.Value : DateTime.MinValue;
            form.UpdateBy = obj.UpdateBy != null ? obj.UpdateBy.Value : 0;
            form.CreateBy = obj.CreateBy != null ? obj.CreateBy.Value : 0;
            return form;
        }
    }
}